package com.hfb.mashibing.alip8.designpattern._015builder;

import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;

public class MainV1 {

    public static void main(String[] args) {
        Terrain t = new ComplexTerrainBuilder()
                        .builderFort()
                        .builderMine()
                        .builderWall()
                        .build();

        Person p = new Person.PersonBuilder()
            .basicInfo(1, "zhangshan", 18)
            .score(20)
            .weight(200)
            .loc("bj","23")
            .build();
        System.err.println(Objects.toString(p));
    }
}
class Person {
    int id;
    String name;
    int age;
    double weight;
    int score;
    Location loc;

    private Person(){}

    public static class PersonBuilder{
        Person p = new Person();

        public PersonBuilder basicInfo(int id, String name,int age) {
            p.id = id;
            p.name = name;
            p.age = age;
            return this;
        }
        public PersonBuilder weight(double weight) {
            p.weight = weight;
            return this;
        }
        public PersonBuilder score(int score) {
            p.score = score;
            return this;
        }
        public PersonBuilder loc(String street, String roomNo) {
            p.loc = new Location(street, roomNo);
            return this;
        }
        public Person build() { return p; }
    }

    @Override
    public String toString() {
        return "Person{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", age=" + age +
            ", weight=" + weight +
            ", score=" + score +
            ", loc=" + loc +
            '}';
    }
}
@Data
@AllArgsConstructor
class Location {
    String street;
    String roomNo;
}
class ComplexTerrainBuilder implements TerrainBuilder {
    Terrain terrain = new Terrain();
    @Override
    public TerrainBuilder builderWall() {
        terrain.wall = new Wall(10,10,50,50);
        return this;
    }

    @Override
    public TerrainBuilder builderFort() {
        terrain.fort = new Fort(10,10,50,50);
        return this;
    }

    @Override
    public TerrainBuilder builderMine() {
        terrain.mine = new Mine(10,10,50,50);
        return this;
    }

    @Override
    public Terrain build() {
        return terrain;
    }
}

/**
 * 地形构建器
 */
interface TerrainBuilder {
    TerrainBuilder builderWall();
    TerrainBuilder builderFort();
    TerrainBuilder builderMine();
    Terrain build();
}

/**
 * 地形
 */
class Terrain {
    Wall wall; // 墙
    Fort fort; // 暗堡
    Mine mine; // 地雷
}
@AllArgsConstructor
class Wall {
    int x,y,w,h;
}
@AllArgsConstructor
class Fort {
    int x,y,w,h;
}
@AllArgsConstructor
class Mine {
    int x,y,w,h;
}
